<?php
    function checkLogin($type = 1){
        if(!$_COOKIE['user']){
            if (empty($_SESSION['wechat_user']) && isWechat()) {
               header("location:./openid.php");
            } else {
               header("location:login.php");
            }
        } else {
            $user = json_decode($_COOKIE['user'],true);
            $query = "select * from whw_user where id=".getInt($user['id']);
            $user = $GLOBALS['objDBCD14']->queryUniqueObject($query);
            $expire=time()+60*60*24*30;
            setcookie("user", json_encode($user), $expire);
            $user = json_decode(json_encode($user),true);
            if(in_array($type,[1,2]) && $user['type'] != $type){
                if($user['type'] == 1){
                    header("location:./index.php");
                } else {
                    header("location:./worker.php");
                }
            }
            return $user;
        }
    }
    function isWechat() {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') !== false) {
           return true;
        } else {
            return false;
        }
        die;
    }

    function getFloat($val){
        return (float)((isset($val)) ? $val : 0);
    }
    function quote($str)
    {
    	return '\'' . getStr($str) . '\'';
    }
     function getInt($val)
    {
    	return (int)((isset($val)) ? $val : 0);
    }
    function getStr($val)
    {
    	return (string)((isset($val)) ? $val : '');
    }
?>